﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.ComponentModel;
using System.Collections;

using System.Windows.Forms;



namespace efuture.utils
{
    public enum EventType
    {
        OnFocus = 1,
        OnBlur = 2,
        OnClosed = 10
    }

    public enum StartupResult
    {
        Failed = 0,
        Success = 1
    }

    public interface IPluginContext
    {
        void OnEvent(EventType e);
    }

    public interface IPlugin
    {

        StartupResult startup(IPluginContext ctx);

        void shutdown();

        void afterPropertiesSet(Dictionary<String, String> properties);

    }

    public partial class InputBox : Form
    {
        private System.ComponentModel.IContainer components = null;

        /// <summary>
        /// Clean up any resources being used.
        /// </summary>
        /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
        protected override void Dispose(bool disposing)
        {
            if (disposing && (components != null))
            {
                components.Dispose();
            }
            base.Dispose(disposing);
        }

        #region Windows Form Designer generated code

        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
            this.edtData = new System.Windows.Forms.TextBox();
            this.labInfo = new System.Windows.Forms.Label();
            this.btnOK = new System.Windows.Forms.Button();
            this.btnCancel = new System.Windows.Forms.Button();
            this.SuspendLayout();
            // 
            // edtData
            // 
            this.edtData.Location = new System.Drawing.Point(12, 91);
            this.edtData.Name = "edtData";
            this.edtData.Size = new System.Drawing.Size(369, 22);
            this.edtData.TabIndex = 0;
            this.edtData.KeyDown += new System.Windows.Forms.KeyEventHandler(this.edtData_KeyDown);
            // 
            // labInfo
            // 
            this.labInfo.AutoEllipsis = true;
            this.labInfo.Location = new System.Drawing.Point(9, 9);
            this.labInfo.Name = "labInfo";
            this.labInfo.Size = new System.Drawing.Size(238, 21);
            this.labInfo.TabIndex = 1;
            this.labInfo.Text = "";
            // 
            // btnOK
            // 
            this.btnOK.Location = new System.Drawing.Point(306, 5);
            this.btnOK.Name = "btnOK";
            this.btnOK.Size = new System.Drawing.Size(75, 23);
            this.btnOK.TabIndex = 2;
            this.btnOK.Text = "确定";
            this.btnOK.UseVisualStyleBackColor = true;
            this.btnOK.Click += new System.EventHandler(this.btnOK_Click);
            // 
            // btnCancel
            // 
            this.btnCancel.Location = new System.Drawing.Point(306, 34);
            this.btnCancel.Name = "btnCancel";
            this.btnCancel.Size = new System.Drawing.Size(75, 23);
            this.btnCancel.TabIndex = 3;
            this.btnCancel.Text = "取消";
            this.btnCancel.UseVisualStyleBackColor = true;
            this.btnCancel.Click += new System.EventHandler(this.btnCancel_Click);
            // 
            // InputBox
            // 
            this.AutoScaleDimensions = new System.Drawing.SizeF(7F, 14F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.ClientSize = new System.Drawing.Size(388, 120);
            this.ControlBox = false;
            this.Controls.Add(this.btnCancel);
            this.Controls.Add(this.btnOK);
            this.Controls.Add(this.labInfo);
            this.Controls.Add(this.edtData);
            this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedDialog;
            this.Name = "InputBox";
            this.Text = "InputBox";
            this.ResumeLayout(false);
            this.PerformLayout();

        }

        #endregion

        private System.Windows.Forms.TextBox edtData;
        private System.Windows.Forms.Label labInfo;
        private System.Windows.Forms.Button btnOK;
        private System.Windows.Forms.Button btnCancel;

        public InputBox()
        {
            InitializeComponent();
        }

        public static string ShowInputBox(String title, String info)
        {
            InputBox box = new InputBox();
            if (!string.IsNullOrEmpty(title))
            {
                box.Text = title;
            }
            if (!string.IsNullOrEmpty(info))
            {
                box.labInfo.Text = info;
            }
            box.ShowDialog();
            return box.edtData.Text;
        }

        private void edtData_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Escape)
            {
                this.edtData.Text = string.Empty;
                this.Close();
            }
            else if (e.KeyCode == Keys.Enter)
            {
                this.Close();
            }
        }

        private void btnOK_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.edtData.Text = string.Empty;
            this.Close();
        }
    }

    public class LabelValue
    {
        public string Label { set; get; }

        public string Value { set; get; }

        public override string ToString()
        {
            return Label;
        }
    }

    public class VdrpUtils
    {
        private static int _MaxValue = 9999999;
        private static int _id = new Random().Next(1, 10000);

        public static void SaveToFile(string path, string sXml)
        {
            FileStream fs = new FileStream(path, FileMode.Create);
            StreamWriter sw = new StreamWriter(fs);
            //开始写入
            sw.Write(sXml);
            //清空缓冲区
            sw.Flush();
            //关闭流
            sw.Close();
            fs.Close();
        }

        public static String getIdentity()
        {
            return String.Format("{0:yyyyMMddHHmmss}{1:d7}", DateTime.Now, (_id++) % _MaxValue);
        }

        public static String Identity
        {
            get { return getIdentity(); }
        }

    }

}
